package com.huike.web.controller.review;


import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.page.TableDataInfo;
import com.huike.review.pojo.Review;
import com.huike.review.service.ReviewService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 该Controller主要是为了复习三层架构以及Mybatis使用的，该部分接口已经放开权限，可以直接访问
 * 同学们在此处编写接口通过浏览器访问看是否能完成最简单的增删改查
 */
@RestController
@RequestMapping("/review")
@Slf4j
public class MybatisReviewController extends BaseController {

    @Autowired
    private ReviewService reviewService;

    /**=========================================================新增数据============================================*/
    /**
     * 添加数据
     * @param name
     * @param age
     * @param sex
     * @return
     */
    @GetMapping("/saveData/{name}/{age}/{sex}")
    public AjaxResult save(@PathVariable String name,@PathVariable Integer age,@PathVariable String sex){
        log.info("name = {},age = {},gender = {}",name,age,sex);

        Review review = new Review();
        review.setName(name);
        review.setAge(age);
        review.setSex(sex);
        int i = reviewService.save(review);

        return AjaxResult.success("成功插入"+i+"条数据");



    }

    /**
     * 请求体添加数据
     * @param review
     * @return
     */
    @PostMapping("/saveData")
    public AjaxResult save2(@RequestBody Review review){

        int i = reviewService.save(review);

        return AjaxResult.success("成功插入"+i+"条数据");



    }


    /**=========================================================删除数据=============================================*/
    /**
     * 根据id删除数据
     * @param id
     * @return
     */
    @DeleteMapping("/remove/{id}")
    public AjaxResult deleteById(@PathVariable Integer id){

        int i = reviewService.deleteById(id);

        return AjaxResult.success("成功删除"+i+"条数据");
    }




    /**=========================================================修改数据=============================================*/
    /**
     * 根据id修改数据
     * @param review
     * @return
     */
    @PostMapping("/update")
    public AjaxResult update(@RequestBody Review review){
        reviewService.updateById(review);

        return AjaxResult.success("修改成功");

    }

    /**=========================================================查询数据=============================================*/

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/getById")
    public AjaxResult getById(@RequestParam Integer id){
      Review review = reviewService.getById(id);

      return AjaxResult.success(review);


    }


    @GetMapping("/getDataByPage")
    public TableDataInfo page(Integer pageNum, Integer pageSize){
//        List<Review> reviewList = reviewService.page(pageNum,pageSize);
//
//        return getDataTablePage(reviewList);

        List<Review> reviewList = reviewService.page(pageNum,pageSize);

        TableDataInfo tablePage =  getDataTablePage(reviewList);
        tablePage.setTotal(reviewService.getTotal());
        return tablePage;


    }






}